#include "../GameLogic/GameManager.h"
#include "HeadLayer.h"

using namespace cocos2d;

bool HeadLayer::OnInit()
{
	SetZOrder(2);
	setContentSize(CCSize(740,1060));
	CCSize size = CCDirector::sharedDirector()->getWinSize();

	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("qiuheti/item2.plist");

	mMainBar = CCSprite::create("ui/main_bar.png");
	if(!mMainBar)
		return false;
	mMainBar->setPosition(ccp(370, 1060-mMainBar->getContentSize().height/2));
	addChild(mMainBar, 0);

	mPutItem = GGameMgr->ItemSpriteFromId(EII_YEZI);
	mPutItem->setPosition(ccp(370, 1060-mMainBar->getContentSize().height/2));
	addChild(mPutItem, 0);

	mDepot = CCSprite::createWithSpriteFrameName("depot.png");
	if(!mDepot)
		return false;
	CCMenuItemSprite* MenuDepot = CCMenuItemSprite::create(mDepot, mDepot, mDepot, this, menu_selector(HeadLayer::menuDepotCallback) );
	const CCPoint& PPut = mPutItem->boundingBox().origin;
	MenuDepot->setPosition(ccp(85,PPut.y+mDepot->getTextureRect().size.height/2));
	CCMenu* pMenu = CCMenu::create(MenuDepot, NULL);
	pMenu->setPosition(CCPointZero);
	addChild(pMenu, 0);

	mStoreItem = CCSprite::create();
	if(!mStoreItem)
		return false;
	mStoreItem->setPosition(ccp(mDepot->getContentSize().width/2,mDepot->getContentSize().height*3/5));
	mDepot->addChild(mStoreItem, 0);

	LLScore = GGameMgr->NumSpriteFromFile("0","qiuheti/over_iconaddnumber-hd.png");
	LLScore->setPosition( ccp(mMainBar->getPosition().x+170,mMainBar->getPosition().y+30));
	LLScore->setAnchorPoint(ccp(.5f,.5f));
	addChild(LLScore, 1);

	LLStep = GGameMgr->NumSpriteFromFile("0","qiuheti/over_iconaddnumber-hd.png");
	LLStep->setPosition( ccp(mMainBar->getPosition().x+170,mMainBar->getPosition().y-30));
	LLStep->setAnchorPoint(ccp(.5f,.5f));
	addChild(LLStep, 1);

	mMainAdd = CCSprite::createWithSpriteFrameName("main_add.png");
	mMainAdd->setVisible(false);
	addChild(mMainAdd,100);

	mMainAddItem = CCSprite::create();
	if(!mMainAddItem)
		return false;
	mMainAddItem->setVisible(false);
	addChild(mMainAddItem, 101);

	setVisible(true);
	return true;
}

void HeadLayer::Flush()
{
	CCSpriteFrame* pFrame = GGameMgr->ItemSpriteFrameFromId(GGameMgr->GetPutItemManager()->GetNowItem());
	CCSpriteFrame* pStoreFrame = GGameMgr->ItemSpriteFrameFromId(GGameMgr->GetPutItemManager()->GetStoreItem());

	MapGrid* pGrid = GGameMgr->GetPutItemManager()->GetGrid();
	if(pGrid)
	{
		mPutItem->setOpacity(127);

		mMainAdd->setVisible(true);
		mMainAdd->setPosition(ccp(pGrid->mRect.origin.x+pGrid->mRect.size.width/2,pGrid->mRect.origin.y+pGrid->mRect.size.height/2));

		mMainAddItem->setVisible(true);
		mMainAddItem->setDisplayFrame(GGameMgr->ItemSpriteFrameFromId(GGameMgr->GetPutItemManager()->GetNowItem()));
		mMainAddItem->setPosition(ccp(pGrid->mRect.origin.x+pGrid->mRect.size.width/2,pGrid->mRect.origin.y+pGrid->mRect.size.height/2));
	}
	else
	{
		mPutItem->setOpacity(255);
		mMainAdd->setVisible(false);
		mMainAddItem->setVisible(false);
	}

	if(pFrame)
		mPutItem->setDisplayFrame(pFrame);

	if(pStoreFrame)
		mStoreItem->setDisplayFrame(pStoreFrame);

	LLStep->setString(CCString::createWithFormat("%d",GGameMgr->GetStep())->getCString());
	LLScore->setString(CCString::createWithFormat("%d",GGameMgr->GetScore())->getCString());
}

void HeadLayer::menuDepotCallback( CCObject* pSender )
{
	GGameMgr->GetPutItemManager()->ExchangeItem(this);
}

